package com.pdd.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.mall.common.api.CommonResult;
import com.pdd.mall.entity.Merchant;
import com.pdd.mall.entity.Order;
import com.pdd.mall.entity.OrderItem;
import com.pdd.mall.service.MerchantService;
import com.pdd.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家订单控制器
 */
@RestController
@RequestMapping("/merchant/orders")
public class MerchantOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private MerchantService merchantService;

    /**
     * 获取订单列表
     */
    @GetMapping
    public CommonResult<Page<Order>> getOrderList(
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Long merchantId = getCurrentMerchantId();
        Page<Order> orderList = orderService.getMerchantOrderList(merchantId, status, pageNum, pageSize);
        return CommonResult.success(orderList);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderNo}")
    public CommonResult<Map<String, Object>> getOrderDetail(@PathVariable String orderNo) {
        Long merchantId = getCurrentMerchantId();
        Order order = orderService.getMerchantOrderDetail(orderNo, merchantId);
        if (order == null) {
            return CommonResult.notFound("订单不存在");
        }

        List<OrderItem> orderItemList = orderService.getOrderItemList(order.getId());

        Map<String, Object> result = new HashMap<>();
        result.put("order", order);
        result.put("orderItems", orderItemList);

        return CommonResult.success(result);
    }

    /**
     * 发货
     */
    @PostMapping("/{orderNo}/ship")
    public CommonResult<String> shipOrder(
            @PathVariable String orderNo,
            @RequestBody(required = false) Map<String, String> params) {
        Long merchantId = getCurrentMerchantId();
        String trackingNo = params != null ? params.get("trackingNo") : null;
        
        boolean success = orderService.shipOrder(orderNo, merchantId, trackingNo);
        if (success) {
            return CommonResult.success("发货成功");
        } else {
            return CommonResult.failed("发货失败，订单不存在或状态不正确");
        }
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/{orderNo}/status")
    public CommonResult<String> updateOrderStatus(
            @PathVariable String orderNo,
            @RequestParam Integer status) {
        Long merchantId = getCurrentMerchantId();
        boolean success = orderService.updateMerchantOrderStatus(orderNo, merchantId, status);
        if (success) {
            return CommonResult.success("更新订单状态成功");
        } else {
            return CommonResult.failed("更新订单状态失败，订单不存在或状态不正确");
        }
    }

    /**
     * 获取当前登录商家ID
     */
    private Long getCurrentMerchantId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Merchant merchant = merchantService.getMerchantByUsername(username);
        return merchant != null ? merchant.getId() : null;
    }
}
